package com.kaishengit.pms.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.pms.entity.Employee;
import com.kaishengit.pms.entity.File1;

import com.kaishengit.pms.util.DBHelp;
import com.kaishengit.pms.util.RowMapper;

public class FileDao {

	private DBHelp<File1> db = new DBHelp<File1>();

	public List<File1> findAllFile1(String filetypeid) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * FROM t_file AS tf ");
		sql.append("LEFT JOIN t_employee AS te ON te.id = tf.employeeid ");
		sql.append("WHERE tf.filetypeid = ? ORDER BY createtime DESC");
		return db.executeQueryForList(sql.toString(), new File1RowMapper(),
				filetypeid);
	}

	class File1RowMapper implements RowMapper<File1> {

		public File1 mapperRow(ResultSet rs) throws SQLException {
			File1 t = new File1();
			t.setId(rs.getString("id"));
			t.setFiletypeid(rs.getString("filetypeid"));
			t.setCreatetime(rs.getString("createtime"));
			t.setEmpoyeeid(rs.getString("employeeid"));
			t.setFilename(rs.getString("filename"));
			t.setName(rs.getString("name"));
			t.setFilesize(rs.getFloat("filesize"));

			Employee em = new Employee();
			em.setId(rs.getString("employeeid"));
			em.setPassword(rs.getString("password"));
			em.setUsername(rs.getString("username"));
			em.setPic(rs.getNString("pic"));
			t.setEmployee(em);

			t.setEmpoyeeid(rs.getString("employeeid"));
			t.setFiletypeid(rs.getString("filetypeid"));

			return t;
		}
	}
	
	
	class FileRowMapper implements RowMapper<File1> {

		public File1 mapperRow(ResultSet rs) throws SQLException {
			File1 t = new File1();
			t.setId(rs.getString("id"));
			t.setFiletypeid(rs.getString("filetypeid"));
			t.setCreatetime(rs.getString("createtime"));
			t.setEmpoyeeid(rs.getString("employeeid"));
			t.setFilename(rs.getString("filename"));
			t.setName(rs.getString("name"));
			t.setFilesize(rs.getFloat("filesize"));
			return t;
		}
	}

	public void sava(File1 f) {
		String sql = "insert into `t_file`(`id`,`name`,`createtime`,filename,filesize,filetypeid,employeeid) values(?,?,?,?,?,?,?)";
		db.executeSQL(sql, f.getId(), f.getName(), f.getCreatetime(),
				f.getFilename(), f.getFilesize(), f.getFiletypeid(),
				f.getEmpoyeeid());

	}

	public void drop(String id) {
		String sql = "DELETE FROM t_file WHERE id = ?";
		db.executeSQL(sql, id);
	}

	public List<File1> findAllFile(String employeeid) {
		String sql = "select * from t_file where employeeid=? ";
		return db.executeQueryForList(sql, new File1RowMapper(), employeeid);
	}

	public File1 findById(String id) {
		String sql = "select * from t_file where id=?";
		return db.executeQueryForObject(sql, new FileRowMapper(), id);
	}

	public void sava(String id, String string, String string2, long l,
			String now, String filetypeid, String id2) {
		String sql = "insert into `t_file`(`id`,`name`,`createtime`,filename,filesize,filetypeid,employeeid) values(?,?,?,?,?,?,?)";
		db.executeSQL(sql, id,string,now,string2,l,filetypeid,id2);
	}

}
